package com.example.controller;

import org.apache.activemq.ScheduledMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.UUID;

@RestController
public class TestController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private JmsTemplate jmsTemplate;

    @RequestMapping("send")
    public String send() {
        int delay = 30000;
        String uuid = UUID.randomUUID().toString();
        logger.warn("发送消息...." + uuid);
        jmsTemplate.send("xxx", new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage();
                message.setText(uuid);
                message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
                return message;
            }
        });
        return uuid;
    }
}
